package com.xiaoyg.direct;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Random;
import java.util.UUID;

/**
 * @author xiaoyg
 * @Title: SendLogDirect
 * @ProjectName rabbitmq-direct
 * @Description: TODO
 * @date 2018/12/416:17
 */

//随机放6条随机类型（Routing key） 的日志给转发器
public class SendLogDirect {

    //交换名称
    private static final String EXCHANGE_NAME = "exchange_logs_direct";

    //日志分类

    private static final String [] SEVERITIES = {"info","warning","error"};

    public static void main(String[] args) throws IOException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.8.129");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setPort(AMQP.PROTOCOL.PORT);

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"direct");

        for (int i = 0; i < 6 ; i++) {

            String str = getStr();

            String message = str+"_log"+ UUID.randomUUID().toString();

            channel.basicPublish(EXCHANGE_NAME,str,null,message.getBytes());

            System.out.println("[a] send '"+message+"'");

            channel.close();
            connection.close();

        }


    }

    private static String getStr(){
        Random random = new Random();
//        int value = random.nextInt(3);
//        System.out.println("队列值为："+value);
        return SEVERITIES[1];

    }

}
